package com.aaa.arouterone.testokhttp

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.*
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

/**
 * @Description:
 * @Author:  lfc
 * @Email:    iamlifuchang@163.com
 * @CreateTime:     2021/3/30 13:58
 * @UpdateRemark:
 */
@Module
@InstallIn(ApplicationComponent::class)
class NetworkModule {

    @Singleton
    //    记得要在provideOkHttpClient()函数的上方加上@Provides注解，这样Hilt才能识别它。
    @Provides
    fun provideOkhttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .build()
    }

    @Singleton
    @Provides
    fun provideRetorfit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
            .baseUrl("http://example.com/")
            .client(okHttpClient)
            .build()
    }
}